#ifndef REQUEST_H_
#define REQUEST_H_

#include <pthread.h>

#include "common_defs.h"

class Request
{

public:

	Request(char *buf, uint length, uint id, result_t &result);

	/*
	 * force_release indicate if we should wait for the waiting threads to woke up
	 * before we destroy the resources.
	 */
	result_t request_release_resources(bool force_release = false);

	/*
	 * make the calling thread stop until the request served - assume the context is
	 * locked before calling this method
	 */
	result_t join_request();

	uint get_request_id();

	void get_buffer_info( char *& buffer, unsigned int &length );

	result_t lock_request();


private:

	// return true if there is a thread waiting for the request's termination
	bool is_someone_waiting( result_t &result );

	char *_buffer;					// local buffer
	uint _length;					// length of the buffer
	uint _id;						// request's ID

	pthread_mutex_t _context_lock;	// lock for the conditional vaiable

	pthread_cond_t _waiting; 		/*< conditional var that indicate if the request still
	 	 	 	 	 	 	 	 		waiting to be served.						>*/

	uint _num_waiting;				/*< counter of the number of threads
										that are waiting for the request's termination >*/

};

#endif /* REQUEST_H_ */
